// pages/my/myInfo/myInfo.js

var utils = require("../../../utils/util.js")
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    doctorInfo: {},
    isEdit: false,

    // 基础信息部分
    avater: "",
    realName: "",
    sex: "",
    birth: 0,
    phone: "",

    showSexPicker: false,
    sexColumns: [
      "男",
      "女"
    ],

    showBirthPicker: false,
    minDate: new Date(1900, 1, 1).getTime(),
    maxDate: new Date().getTime(),

    // 医生相关部分
    department: 0,
    level: 0,
    workTime: 0,
    desc: "",
    advantage: "",

    showDepartmentPicker: false,
    departments: [
      "骨科",
      "心理科",
      "耳科",
      "咽喉科",
      "心血管科",
      "泌尿科",
      "呼吸科",
      "消化科",
      "口腔科",
      "儿科",
      "妇科",
      "神经科" 
    ],

    showLevelPicker: false,
    levelColumns: [
      "教授",
      "副教授",
      "主任医师",
      "副主任医师",
      "主治医师",
      "副主治医师"
    ],
  },

  // 编辑状态获取姓名
  onInputRealname(event) {
    console.log("onInputRealname", event);
    this.setData({
      realName: event.detail.value
    });
  },

  // 性别选择相关函数
  onClickSex() {
    if (this.data.isEdit == false)  return;

    this.setData({
      showSexPicker: true
    });
  },

  onConfirmSex(event) {
    console.log("onConfirmSex", event);

    this.setData({
      sex: event.detail.value,
      showSexPicker: false
    });
  },

  onCancelSex() {
    console.log("onCancelSex");

    this.setData({
      showSexPicker: false
    });
  },

  // 出生日期选择相关函数
  onClickBirth() {
    if (this.data.isEdit == false)  return;

    this.setData({
      showBirthPicker: true
    });
  },

  onCancelBirth() {
    console.log("onCancelSex");

    this.setData({
      showBirthPicker: false
    });
  },

  onConfirmBirth(event) {
    console.log("onConfirmBirth", event);

    this.setData({
      birth: event.detail,
      showBirthPicker: false
    });
  },

  // 编辑状态获取手机号
  onInputPhone(event) {
    console.log("onInputPhone", event);

    this.setData({
      phone: event.detail
    });
  },

  // 科室选择有关
  onClickDepartment(event) {
    if (this.data.isEdit == false)  return;

    this.setData({
      showDepartmentPicker: true
    });
  },

  onCancelDepartment(event) {
    console.log("onCancelDepartment");

    this.setData({
      showDepartmentPicker: false
    });
  },

  onConfirmDepartment(event) {
    console.log("onConfirmDepartment", event);

    this.setData({
      department: event.detail.index,
      showDepartmentPicker: false
    });
  },

  // 职称选择有关
  onClickLevel(event) {
    if (this.data.isEdit == false)  return;

    this.setData({
      showLevelPicker: true
    });
  },

  onCancelLevel(event) {
    console.log("onCancelLevel");

    this.setData({
      showLevelPicker: false
    });
  },

  onConfirmLevel(event) {
    console.log("onConfirmLevel", event);

    this.setData({
      level: event.detail.index,
      showLevelPicker: false
    });
  },

  onInputWorktime(event) {
    console.log("onInputWorktime", event);
    this.setData({
      workTime: parseInt(event.detail)
    });
  },

  onInputDesc(event) {
    console.log("onInputDesc", event);
    this.setData({
      desc: event.detail
    });
  },

  onInputAdvantege(event) {
    console.log("onInputAdvantege", event);
    this.setData({
      advantage: event.detail
    });
  },

  // 点击编辑按钮
  onEdit(event) {
    const userInfo = this.data.userInfo;
    const doctorInfo = this.data.doctorInfo;

    this.setData({
      isEdit: true,

      avater:   userInfo.avater,
      realName: userInfo.realName,
      sex:      userInfo.sex,
      birth:    userInfo.birth,
      phone:    userInfo.phone,

      department: doctorInfo.department,
      level:      doctorInfo.level,
      workTime:   doctorInfo.workTime,
      desc:       doctorInfo.desc,
      advantage:  doctorInfo.advantage
    });
  },

  // 点击取消按钮
  onCancel() {
    this.setData({
      isEdit: false,
    });
  },

  // 点击保存按钮
  onSave(event) {
    console.log("onSave", this.data);

    this.updateUserInfo();
    this.updateDoctorInfo();

    this.setData({
      isEdit: false
    });
  },

  updateUserInfo() {
    let userInfo      = this.data.userInfo;
    userInfo.avater   = this.data.avater;
    userInfo.realName = this.data.realName;
    userInfo.sex      = this.data.sex;
    userInfo.birth    = this.data.birth;
    userInfo.phone    = this.data.phone;

    this.setData({ userInfo: userInfo });   // 更新本地 userInfo
    app.setGlobalData(userInfo);            // 更新全局 userInfo

    // 更新数据库的 userInfo
    wx.cloud.callFunction({
      name: 'update_user_info',
      data: {
        userInfo: userInfo
      },
      success: res => {
        console.log("update user_info success: ", res);
      },
      fail: res => {
        console.log("update user_info fail: ", res);
      },
    });
  },

  updateDoctorInfo() {
    let doctorInfo        = this.data.doctorInfo;
    doctorInfo.department = this.data.department;
    doctorInfo.level      = this.data.level;
    doctorInfo.workTime   = this.data.workTime;
    doctorInfo.desc       = this.data.desc;
    doctorInfo.advantage  = this.data.advantage;

    this.setData({ doctorInfo: doctorInfo});  // 更新本地 doctorInfo

    // 更新数据库 doctorInfo
    wx.cloud.callFunction({
      name: 'update_doctor_info',
      data: {
        doctorInfo: doctorInfo
      },
      success: res => {
        console.log("update doctor_info success: ", res);
      },
      fail: res => {
        console.log("update doctor_info fail: ", res);
      },
    });
  },

  // 设置 doctorInfo
  setDoctorInfo(id) {
    wx.cloud.callFunction({
      name: 'get_doctorInfo',
      data: {
        id: id
      },
      success: res => {
        console.log("success", res);
        if (res.result.data.length == 0)  return;

        this.setData({
          doctorInfo: res.result.data[0]
        });
      },
      fail: res => {
        console.error("get userInfo from DB fail.", res);
      }
    });
  },

  // 生命周期函数--监听页面加载
  onLoad(options) {
    console.log("===== myInfo onLoad");

    this.setData({ userInfo: app.globalData.userInfo });

    const that = this;
    if(this.data.userInfo.type == 2) {
      console.log("_id", that.data.userInfo._id);
      that.setDoctorInfo(that.data.userInfo._id);
    }
  },

  // 生命周期函数--监听页面初次渲染完成
  onReady() { console.log("===== myInfo onReady"); },

  // 生命周期函数--监听页面显示
  onShow() { console.log("===== myInfo onShow"); },

  // 生命周期函数--监听页面隐藏
  onHide() {  },

  // 生命周期函数--监听页面卸载
  onUnload() { console.log("===== myInfo onUnload"); },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() { },

  // 页面上拉触底事件的处理函数
  onReachBottom() { },

  // 用户点击右上角分享
  onShareAppMessage() { }
})